/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

import data.PanitiaData;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Muhammad Dony
 */
public class PanitiaDB extends ConnectDB {

    private String sql;
    private Connection con;
    private Statement stmt;
    private ResultSet rs;
    
    public List getpanitia() throws SQLException, ClassNotFoundException{
        sql = "SELECT * from `panitia`";
        return getpanitiadata(sql);
    }
    
    public List getpanitiaById(String ID) throws SQLException, ClassNotFoundException {
        sql = "SELECT * from `panitia` WHERE `ID`='"+ID+"';";
        return getpanitiadata(sql);
    }
        
    public List getpanitiaByxx(String ID, int no) throws SQLException, ClassNotFoundException {
        sql = "SELECT * from `panitia` WHERE `ID`='"+ID+"' AND `nomor`='"+no+"';";
        return getpanitiadata(sql);
    }
    
     private List getpanitiadata(String sql) throws SQLException, ClassNotFoundException{
        // mempersiapkan list dataPelaporan
        List listpanitia = new ArrayList();
        //membuat koneksi , createConnection terdapat dalam DBClass
        con = createConnection();
        stmt = con.createStatement();
        rs = stmt.executeQuery(sql);
        // mempersiapkan object dataPelaporan
        PanitiaData panitiaUser;
        
        while(rs.next()){
            panitiaUser = new PanitiaData();
            panitiaUser.setNomor(rs.getInt(1));
            panitiaUser.setID(rs.getString(2));
            panitiaUser.setKegiatan(rs.getString(3));
            panitiaUser.setJabatan(rs.getString(4));
            panitiaUser.setTingkat(rs.getString(5));
            panitiaUser.setTahun(rs.getString(6)); 
            listpanitia.add(panitiaUser);
        }
        // menutup semua koneksi db
        rs.close();
        stmt.close();
        con.close();
        //return value list data pelaporan
        return listpanitia;
        
    }
    
    private void actpanitia(String sql) throws ClassNotFoundException, SQLException{
        // membuat koneksi
        con = createConnection();
        stmt = con.createStatement();
        // execute query
        stmt.execute(sql);
        // menutup koneksi db
        stmt.close();
        con.close();
    }
   public void insertPanitia(PanitiaData listPanitia) throws ClassNotFoundException, SQLException{
        // query
        sql = "INSERT INTO `scholarship`.`panitia` (`ID`, `kegiatan`, `jabatan`, `tingkat`, `tahun`) VALUES ('"
                +listPanitia.getID()+"', '"
                +listPanitia.getKegiatan()+"', '"
                +listPanitia.getJabatan()+"', '"
                +listPanitia.getTingkat()+"', '"
                +listPanitia.getTahun()+"');";
        actpanitia(sql);
    }
   

   //// EDIT DAN DELETE MASIH BERMASALAH Y!!!!!
   
   
    public void updatePanitia(PanitiaData listPanitia) throws ClassNotFoundException, SQLException{
        // query
        sql = "UPDATE `panitia` SET `kegiatan`='"+listPanitia.getKegiatan()+
                "',`jabatan`='"+listPanitia.getJabatan()+
                "',`tingkat`='"+listPanitia.getTingkat()+
                "',`tahun`='"+listPanitia.getTahun()
                +"' WHERE `ID`='"+listPanitia.getID()+"'AND nomor="+listPanitia.getNomor()+";";
        // memanggil method untuk execute query
        actpanitia(sql);
    }
    public void deletePanitia(String ID, int kegiatan) throws ClassNotFoundException, SQLException{
        sql = "DELETE FROM `panitia` WHERE `ID`='"+ID+"' AND `nomor`='"+kegiatan+"';";
        actpanitia(sql);
    } 
    
    
    
}
